<?php
Class Controller_Manage_Pf extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '创建批发商';
		$vegebusinesstype = VegeBusinessType::find()->asArray()->getAll();
		$this->_view['vegebusinesstype'] = $vegebusinesstype;
		$notetype = NoteType::find()->asArray()->getAll();
		$this->_view['notetype'] = $notetype;
		//获取主体信息
		$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
		
		$exsit = BusinessBaseInfo::find()->order('id desc')->asArray()->getOne();
		if(isset($exsit['id'])&&$exsit['id'])
		{
			$number = ((int)substr($exsit['businessnumber'],-4))+1;
			$number = $baseinfo['companynumber'].str_pad($number,4,0,STR_PAD_LEFT);
		}
		else
		{
			$number = $baseinfo['companynumber'].'0001';
		}
		
		$this->_view['number'] = $number;
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '创建批发商保存';
		$businessnumber = isset($_POST['businessnumber'])?$_POST['businessnumber']:'';
		$businessname = isset($_POST['businessname'])?$_POST['businessname']:'';
		$regnum = isset($_POST['regnum'])?$_POST['regnum']:'';
		$identity = isset($_POST['identity'])?$_POST['identity']:'';
		$nodetypename = isset($_POST['nodetypename'])?$_POST['nodetypename']:'';
		$nodetype = isset($_POST['nodetype'])?$_POST['nodetype']:'';
		$recorddate = isset($_POST['recorddate'])?$_POST['recorddate']:'';
		$corporate = isset($_POST['corporate'])?$_POST['corporate']:'';
		$phone = isset($_POST['phone'])?$_POST['phone']:'';
		$time = date('Y-m-d H:i:s');
		
		if(strlen($phone)<11)
		{
			return $this->_redirectMessage('提交失败', "请填写正确的手机号码", url('Manage::Pf/Create'),FAILURE);
		}
		
		$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
		
		if($businessnumber && $businessname)
		{
			$exsit = BusinessBaseInfo::find('businessnumber=? or businessname=?',$businessnumber,$businessname)->asArray()->getOne();
			if(isset($exsit['id'])&&$exsit['id'])
			{
				return $this->_redirectMessage('提交失败', "对应名称或编号的批发商信息已经存在", url('Manage::Pf/Create'),FAILURE);
			}
			
			$new = new BusinessBaseInfo();
			$new->companynumber = $baseinfo['companynumber'];
			$new->businessnumber = $businessnumber;
			$new->businessname = $businessname;
			$new->regnum = $regnum;
			$new->identity = $identity;
			$new->nodetypename = $nodetypename;
			$new->nodetype = $nodetype;
			$new->recorddate = $recorddate;
			$new->corporate = $corporate;
			$new->phone = $phone;
			$new->time = $time;
			$new->updatetime = $time;
			$new->password = md5(substr($phone,-6));//批发商编码默认手机后六位
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::Pf/Create'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "编号和名称不能为空", url('Manage::Pf/Create'),FAILURE);
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '批发商信息列表';
		$vegebusinesstype = VegeBusinessType::find()->asArray()->getAll();
		$this->_view['vegebusinesstype'] = $vegebusinesstype;
		$notetype = NoteType::find()->asArray()->getAll();
		$this->_view['notetype'] = $notetype;
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = BusinessBaseInfo::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Pf/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '批发商信息详情';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$product = BusinessBaseInfo::find('id=?', $id)->asArray()->getOne();
		if ($product)
		{
			$this->_view['product'] =  $product;
		}
		else
		{
			return $this->_redirectMessage('查询失败', '提交参数错误', url('Manage::Pf/List'), FAILURE);
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '批发商编辑';
		$vegebusinesstype = VegeBusinessType::find()->asArray()->getAll();
		$this->_view['vegebusinesstype'] = $vegebusinesstype;
		$notetype = NoteType::find()->asArray()->getAll();
		$this->_view['notetype'] = $notetype;
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$businessbaseinfo = BusinessBaseInfo::find('id=?', $id)->asArray()->getOne();
		if ($businessbaseinfo)
		{
			$this->_view['businessbaseinfo'] = $businessbaseinfo;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '批发商不存在', url('Manage::Pf/List'), FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '批发商编辑保存';
		if (isset($_POST)&&$_POST)
		{
			$id = isset($_POST['id'])?$_POST['id']:'';
			$businessnumber = isset($_POST['businessnumber'])?$_POST['businessnumber']:'';
			$businessname = isset($_POST['businessname'])?$_POST['businessname']:'';
			$regnum = isset($_POST['regnum'])?$_POST['regnum']:'';
			$identity = isset($_POST['identity'])?$_POST['identity']:'';
			$nodetypename = isset($_POST['nodetypename'])?$_POST['nodetypename']:'';
			$nodetype = isset($_POST['nodetype'])?$_POST['nodetype']:'';
			$recorddate = isset($_POST['recorddate'])?$_POST['recorddate']:'';
			$corporate = isset($_POST['corporate'])?$_POST['corporate']:'';
			$phone = isset($_POST['phone'])?$_POST['phone']:'';
			$password = isset($_POST['password'])?$_POST['password']:'';
			$time = date('Y-m-d H:i:s');
			
			if(strlen($phone)<11)
			{
				return $this->_redirectMessage('提交失败', "请填写正确的手机号码", url('Manage::Pf/Edit'),FAILURE);
			}
			
			if($businessname && $nodetypename && $id && $phone)
			{
				$edit = BusinessBaseInfo::find('id=?', $id)->getOne();
				$edit->businessname = $businessname;
				$edit->regnum = $regnum;
				$edit->identity = $identity;
				$edit->nodetypename = $nodetypename;
				$edit->nodetype = $nodetype;
				$edit->recorddate = $recorddate;
				$edit->corporate = $corporate;
				$edit->phone = $phone;
				$edit->time = $time;
				$edit->updatetime = $time;
				if($password)
				{
					$edit->password = md5($password);//批发商编码默认手机后六位
				}
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);//只返回上一页即可，这样好定位
			}
			else
			{
				return $this->_redirectMessage('提交失败', "编号和名称不能为空", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Pf/List'), FAILURE);
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '删除';
		$result = BusinessBaseInfo::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionDeleteAll()
	{
		$this->_view['title'] = '勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = BusinessBaseInfo::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}

    function actionStop()
    {
        $this->_view['title'] = '停用';
        //echo  $_GET['id'];exit;
        $result = Code::find(array('id'=>$_GET['id']))->getOne();
        if ($result)
        {
            $result->status = 2;
            $result->save();
            echo 1;exit;
        }
        else
        {
            echo 0;exit;
        }
    }
	
	function actionExportBusinessBaseInfo()
	{
		$this->_view['title'] = '导出批发商信息';
		set_time_limit(0);
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$businessbaseinfo = BusinessBaseInfo::find()->asArray()->getAll();
		//dump($businessbaseinfo);exit;
		if ($businessbaseinfo)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] . '_KHDA.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($businessbaseinfo as $bbi)
			{
				$line = ",".$bbi['companynumber'];
				$line .= ",".$bbi['businessnumber'];
				$line .= ",".$bbi['businessname'];
				$line .= ",".$bbi['regnum'];
				$line .= ",".$bbi['nodetypename'];
				$line .= ",".$bbi['nodetype'];
				$line .= ",".str_replace('-','',$bbi['recorddate']);
				$line .= ",".$bbi['corporate'];
				$line .= ",".$bbi['phone'];
				$line .= ",".str_replace('-','',$bbi['updatetime']);
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Pf/List'), FAILURE);
		}
	}
}